package top.songxs.yygh.service.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.songxs.yygh.enums.RefundStatusEnum;
import top.songxs.yygh.model.order.PaymentInfo;
import top.songxs.yygh.model.order.RefundInfo;
import top.songxs.yygh.service.order.mapper.RefundMapper;
import top.songxs.yygh.service.order.service.RefundService;

import java.util.Date;

@Service
public class RefundServiceImpl extends ServiceImpl<RefundMapper, RefundInfo> implements RefundService {
    @Override
    public RefundInfo saveRefundInfo(PaymentInfo paymentInfo) {
        //1、根据订单id查询退款记录
        Long orderId = paymentInfo.getOrderId();
        QueryWrapper<RefundInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id", orderId);
        RefundInfo refundInfo = this.baseMapper.selectOne(wrapper);
        //如果退款记录存在，直接返回
        if (refundInfo != null) return refundInfo;
        //2、如果不存在，创建退款记录
        refundInfo = new RefundInfo();



/*        refundInfo.setOrderId(paymentInfo.getOrderId());
        refundInfo.setPaymentType(paymentInfo.getPaymentType());
        refundInfo.setOutTradeNo(paymentInfo.getOutTradeNo());
        refundInfo.setRefundStatus(RefundStatusEnum.UNREFUND.getStatus());//未退款
        refundInfo.setSubject(paymentInfo.getSubject());
        refundInfo.setTotalAmount(paymentInfo.getTotalAmount());
        refundInfo.setCreateTime(new Date());
        refundInfo.setUpdateTime(new Date());*/


        refundInfo.setOutTradeNo(paymentInfo.getOutTradeNo());  //订单号  3
        refundInfo.setOrderId(orderId); //订单id  1
        refundInfo.setPaymentType(paymentInfo.getPaymentType());//支付方式  2
        refundInfo.setTotalAmount(paymentInfo.getTotalAmount());//总支付金额  6
        refundInfo.setSubject(paymentInfo.getSubject());//支付详情   5
        refundInfo.setRefundStatus(RefundStatusEnum.UNREFUND.getStatus());//未退款  4
        refundInfo.setCreateTime(new Date());
        refundInfo.setUpdateTime(new Date());
        //利用支付记录的属性，创建退款记录
        this.baseMapper.insert(refundInfo);

        return refundInfo;
    }
}
